const { regeneratorRuntime, dayjs } = global

Page(global.connect(
  state => ({
    ...state.activityReducer,
    ...state.globalReducer,
  }),
  dispatch => ({
    async ayncMakeOrderForm(data) {
      return await dispatch(global.activityReducer.ayncMakeOrderForm(data))
    },
    async getOrderDetailByOrderNo(data) {
      return await dispatch(global.activityReducer.getOrderDetailByOrderNo(data))
    },
    async getActivityImageList(data) {
      return await dispatch(global.activityReducer.getActivityImageList(data))
    },
    async getMaterial(data) {
      return await dispatch(global.activityReducer.getMaterial(data))
    },
    async getActivityDetail(data) {
      return await dispatch(global.activityReducer.getActivityDetail(data))
    },
    async prepareEffectImage(data) {
      return await dispatch(global.globalReducer.prepareEffectImage(data))
    },
    async getStorePosition(data) {
      return await dispatch(global.activityReducer.getStorePosition(data))
    },
    async asyncDetailToPosition(data) {
      return await dispatch(global.activityReducer.asyncDetailToPosition(data))
    },
  })
)({
  async onLoad(option) {
    console.log(this.route, option)

    // step1 后去订单信息
    const { store_id, order_no, update } = option
    const orderDetailRes = await this.getOrderDetailByOrderNo({ order_no })

    // step2 获取活动信息
    const activity_id = orderDetailRes.data.activity_id
    this.getActivityDetail({ activity_id })

    // step3 获取活动所有效果图
    this.getActivityImageList({ activity_id })

    // step4 获取材料列表
    this.getMaterial()

    // step5 获取广告位信息
    await this.getStorePosition({ store_id })

    // step6 是否编辑
    update && this.asyncDetailToPosition()
  },
  handleLicon1Tap() {
    wx.navigateBack()
  },
  handleCalendar(e) {
    console.log('handleCalendar', e)
    const { title, range } = e.currentTarget.dataset
    const { uiActivityUpTime } = this.data
    this.selectComponent('#calendar').handleShow(null, {
      title, range,
      startDate: uiActivityUpTime
    })
  },
  handleCalendarHide(e) {
    console.log('handleCalendarHide', e)
    const { title } = e.detail
    if (title === '下画时间') {
      this.selectComponent('#switch').handleChecked(false)
    }
  },
  handleConfirmDialog(e) {
    console.log('handleConfirmDialog', e)
    const { cb, data } = e.detail
    const { title, startDate } = data
    switch (title) {
      case '上画时间':
        {
          this.ayncMakeOrderForm({
            up_time: dayjs(startDate).unix(),
            uiActivityUpTime: startDate
          })
          cb && cb()
          break;
        }
      case '下画时间':
        {
          this.ayncMakeOrderForm({
            down_time: dayjs(startDate).unix(),
            uiActivityDownTime: startDate
          })
          cb && cb()
          break;
        }
      default:
        break;
    }

  },
  handleSwitch(e) {
    console.log('handleSwitch', e)
    const checked = e.detail
    if (checked) {
      // const { uiActivityDownTime } = this.data
      // this.selectComponent('#calendar').handleShow(null, {
      //   title: '下画时间',
      //   range: false,
      //   startDate: uiActivityDownTime
      // })

      const ae = Number(this.data.activityDetail.data.activity_end_time)  * 1000;
      const dt = dayjs(ae).add(1, 'days');
      this.ayncMakeOrderForm({
        down_time: dt.unix(),
        uiActivityDownTime: dt.format('YYYY-MM-DD')
      })
    } else {
      // this.setData({
      //   uiActivityDownTime: ''
      // })
      this.ayncMakeOrderForm({
        down_time: '',
        uiActivityDownTime: ''
      })
    }
  },
  handleViewEffect(e) {
    console.log('handleViewEffect', e)
    const urls = this.data.activityImageList.data.map(i => {
      return {
        name: i.image_name,
        url: i.image_url
      }
    })
    this.prepareEffectImage(urls)
    global.handleRoute({ route: 'effect' })
  },
  handleNextStep(e) {
    console.log('handleNextStep', e)
    // if (!this.data.orderDetail.data.up_time) {
    //   this.selectComponent('#toast').handleShow({
    //     message: '请选择上画日期'
    //   })
    //   return
    // }
    global.handleRoute({ route: 'make-order-step2' })
  }
}))